<?php
include($_SERVER["DOCUMENT_ROOT"] . "/bob.php");
include($_SERVER["DOCUMENT_ROOT"] . "/fonctions_globales.php");
$mysqli = new_mysqli($host, $user, $password, $database); //connection à la DB

$variable_get = filter_input_array(INPUT_GET);

//Pagination - Initialisation
$tri = "nb_territoires";
$order = "DESC";

if (isset($variable_get['select_tri'])) {
    $tri = $variable_get['select_tri'];
    $order = $variable_get['select_order'];
}




if (isset($variable_get['page'])) {
    $page = $variable_get['page'];
} else {
    $page = 1;
}
$nb_message_par_page = 60;
$retour_total = mysqli_query_with_error($mysqli, "SELECT * FROM gens ORDER BY " . $tri . " " . $order);

$total = $retour_total->num_rows;

$nombreDePages = ceil($total / $nb_message_par_page);
$pageActuelle = intval($page);
if ($pageActuelle > $nombreDePages) {
    $pageActuelle = $nombreDePages;
}
$premiereEntree = ($pageActuelle - 1) * $nb_message_par_page;
?>


<?php
main_body($mysqli, "Liste des joueurs", "formulaire;bandeau;forum;liste_joueurs", "", false, true);
?>

<div id='background_corps'> </div>
<div id="corps">
    <div id="contenus">
        <div id="joueurs">
            <h3>Liste des joueurs</h3>
            <form id="tri" action="membres.php">
                <span class="select droit page">
                    <p class="page">Aller à la page&nbsp;</p>
                    <select class="classic ombre  submit" onchange="document.location.href = ('membres.php?select_tri=<?php echo $tri ?>&select_order=<?php echo $order ?>&page=' + document.getElementById('select_page').value)" id="select_page" name="select_page">
                        <?php
                        for ($i = 1; $i <= $nombreDePages; $i++) {
                            $selected = "";
                            if ($pageActuelle == $i) {
                                $selected = 'selected="selected"';
                            }
                            echo '<option value="' . $i . '" ' . $selected . '>' . $i . '</option>';
                        }
                        ?>
                    </select>
                </span>

                <div class="col_g">
                    <label>Afficher par</label> 
                    <select name="select_tri" class="classic ombre submit" class="forum ombre" id="select_tri">
                        <option value="username" <?php echo ($tri=="username")? 'SELECTED':'' ;?>>Pseudo (ordre alphabétique)</option>
                        <option value="faction" <?php echo ($tri=="faction")? 'SELECTED':'' ;?>>Faction</option>                        
                        <option value="nb_territoires" <?php echo ($tri=="nb_territoires")? 'SELECTED':'' ;?>>Nombre de territoires</option>
                        <option value="game_level" <?php echo ($tri=="game_level")? 'SELECTED':'' ;?>>Niveau</option>                        
                        <option value="user_posts" <?php echo ($tri=="user_posts")? 'SELECTED':'' ;?>>Nombre de posts</option>
                        <option value="user_regdate"<?php echo ($tri=="user_regdate")? 'SELECTED':'' ;?>>Date d’inscription</option>
                    </select>
                </div>
                <div class="tri">
                    <p class="label">Par ordre chronologique</p>
                    <input type="radio" name="select_order" value="ASC" id="croissant" class="input_reponse submit" <?php echo (($order == 'ASC') ? 'CHECKED' : ""); ?> />

                    <label for="croissant">croissant</label>
                    <input type="radio" name="select_order" value="DESC" id="decroissant" class="input_reponse submit" <?php echo (($order == 'DESC') ? 'checked' : ""); ?> />
                    <label for="decroissant">décroissant</label>
                </div>            
            </form>
            <div class="ombre clear">

                <ul id="entete_forum">
                    <li class="pseudo"><span>Pseudo</span></li>
                    <li class="faction milieu">Faction</li>
                    <li class="territoires milieu">Territoires</li>
                    <li class="niveau milieu">Niveau</li>
                    <li class="posts milieu">Posts</li>
                    <li class="inscription milieu">Inscription</li>
                    <li class="cauchemard"><span>Pire cauchemar</span></li>
                </ul>

                <?php
                $rqt_joueurs = "SELECT g.user_id as userid, g.user_avatar_min as avatar_min, g.couleur as couleur, g.username  as pseudo, g.faction  as faction, g.nb_territoires  as territoires, g.game_level as niveau, g.user_posts  as posts, g.user_regdate  as inscription, g.cauchemar as cauchemar_id, g2.username as cauchemar_username FROM gens as g LEFT JOIN gens as g2 ON g.cauchemar=g2.user_id ORDER BY g." . $tri . " " . $order . " LIMIT " . $premiereEntree . "," . $nb_message_par_page;
                $result = mysqli_query_with_error($mysqli, $rqt_joueurs);
                while ($ligne = $result->fetch_assoc()) {
                    // var_dump($ligne);
                    ?>
                    <div class="ligne bordure">
                        <div class="pseudo">
                            <a href="profil.php?id=<?php echo $ligne['userid'] ?>"><?php echo get_avatar($ligne['avatar_min'], $ligne['couleur'], "", "", 30, "") ?><?php echo $ligne['pseudo'] ?></a><!-- pourquoi dans les autres version je n'ai pas mis le background color direct ici ? Oo -->
                        </div>
                        <div class="faction milieu">
                            <?php if ($ligne['faction'] != "") {
                                ?>
                                <img title="<?php echo $ligne['faction'] ?>" src="../images/<?php echo $ligne['faction'] ?>.png"/>
                                <?php
                            }else{
                                ?>
                                <img src="../images/no_faction.png"/>
                                <?php
                            }
                            ?>
                        </div>
                        <div class="territoires milieu">
                            <strong><?php echo $ligne['territoires']?></strong>
                        </div>
                        <div class="niveau milieu">
                            <strong><?php echo $ligne['niveau']?></strong>
                        </div>
                        <div class="posts milieu">
                            <strong><?php echo get_num($ligne['posts'])?></strong>
                        </div>
                        <div class="inscription milieu">
                            <p>le <?php echo datouille_slash($ligne['inscription']) ?></p>
                    </div>
                        <div class="cauchemard">
                            
                            <a href="profil.php?id=<?php echo $ligne['cauchemar_id'] ?>"><?php echo $ligne['cauchemar_username'] ?></a>
                    </div>
                    </div>

                    <?php
                }
                ?>               

            </div>
            <?php 
                pagination_bas('membres.php?select_tri='.$tri.'&select_order='.$order.'&', 'page=', $nombreDePages, $page);
                
            ?>
        </div>
    </div>
</div>
<script>
    $('.submit').change(function() {
        //alert('salut');
        var ordre = $('input[name=select_order]:checked').val();
        document.location.href = ('membres.php?select_tri=' + document.getElementById('select_tri').value + '&select_order=' + ordre + '&page=' + document.getElementById('select_page').value);
    });


</script>
<?php
show_footer()
//recherche des gens concernés
//$result = mysqli_query_with_error($mysqli, "SELECT * FROM gens WHERE user_id > '0' ORDER BY $trii $ordrr");
//$data_pages = pagination(30, $result->num_rows, "membres.php?page=$page2&tri=$trii&ordr=$ordrr");
//
//echo "<br><br><div style=width: 300px;'><form action='membres.php' method='POST'> Trier par: <select name='tri' class='noir'>
//<option value='user_regdate'>Inscription<option value='username'";
//if ($trii == 'username')
//    echo" selected ";
//echo ">Pseudo<option value='user_posts'";
//if ($trii == 'user_posts')
//    echo" selected ";
//echo ">Nb de posts
//</select> 
//Ordre: <select name='ordr' class='noir'><option value='DESC'>Decroissant
//<option value=''";
//if ($ordrr == "")
//    echo" selected ";
//echo ">Croissant</select> <input type='submit' value='Go'></form></div>";
//
//echo "<div class='ligne' style='border-top: 2px ridge #555555; font-weight: bold;
//padding-bottom: 3px; text-align:center; margin-top:5px;'>
//<div style='width:30px; float:left;'>N°</div><div style='width:13%; float:left;'>
//Pseudo</div><div style='width:37%; float:left;'>
//Notes</div><div style='width:20%; float:left;'>Localisation</div>
//<div style='width:10%; float:left;'>Inscription</div>
//<div style='width:7%; float:left;'>Posts</div>
//<div style='width:9%; float:left;'>Site web</div></div>";
//
//$nb_current = 0;
//while ($ligne = $result->fetch_assoc()) { //affichage du tableau des membres
//    $nb_current++;
//    if ($nb_current <= $data_pages['nbmin'])
//        continue;
//    if ($nb_current > $data_pages['nbmax'])
//        break;
//
//    $not = get_data_propre_sortie_db($ligne['notes']);
//    if ($not == "")
//        $not = "-";
//
//    $lieu = get_data_propre_sortie_db($ligne['user_from']);
//    if ($lieu == "")
//        $lieu = "-";
//
//    $site = get_data_propre_sortie_db($ligne['user_website']);
//    if ($site == "")
//        $site = "-";
//    else {
//        if (strpos($site, "http://") !== 0)
//            $site = "http://" . $site;
//        $site = "<a href='$site' target='_blank'><img src='images/icon_www.gif' border='0'></a>";
//    }
//
//    $datp = datouille($ligne['user_regdate'], false);
//    $blaze = get_data_propre_sortie_db($ligne['username']);
//    echo "<div class='ligne' style='border-bottom: none; padding-top: 10px;'><div style='float:left; width: 30px; text-align: center;'>
//	 $nb_current</div><div style='float:left; background: #220000; text-align:center; width: 13%;'><a href='profil.php?id={$ligne['user_id']}'>$blaze
//	 </a></div><div style='float:left; width: 37%; text-align:center;'>$not</div>
//	 <div style='float:left; background: #220000; text-align: center; width: 20%;'>$lieu</div>
//	 <div class='petit' style='float:left; text-align:center; width: 10%;'>$datp</div>
//	 <div style='width:7%; float:left; background: #220000; text-align:center;'>{$ligne['user_posts']}</div>
//	 <div style='text-align:center; float:left; width: 9%;'>$site</div></div>";
//}
//
//echo"<div style='border-top: 2px ridge #555555; margin-right:2px;'></div>";
//pagination_bas($data_pages, $nb_current);
?>
